package com.zheng.russiaRec.model.utils 
{
	import com.zheng.russiaRec.controller.C_AddScore;
	
	/**
	 * @describe  	...
	 * @author  	Mr.zheng
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class U_GetInfoFromArr
	{
		/**
		 * 
		 * @param	arr1	根据arr1取列编号范围
		 * @param	arr2
		 * @return
		 */
		public function getMinDis(arr1:Array,arr2:Array,maxNum:int):int {
			var colsScope:Array = getColsScope(arr1);
			var arr1Max:Array = getRecVosByArr(arr1, colsScope, true);
			var arr2Min:Array = getRecVosByArr(arr2, colsScope, false);
			var disNum:int = 1000;
			if (arr2Min.length <= 0) {
				disNum = maxNum - getRecForMaxRow(arr1);
				return disNum;
			}
			for (var i:int = 0; i < arr1.length; i++) 
			{
				var recVo1:Array = arr1[i] as Array;
				var recVo2:Array = [];
				var num:int;
				for (var j:int = 0; j < arr2.length; j++) 
				{
					recVo2 = arr2[j] as Array;
					if (recVo1[0] == recVo2[0]) {
						num = getDis(recVo1, recVo2);
						if (num < disNum) {
							disNum = num;
						}
					}
				}
			}
			return disNum;
		}
		
		/**
		 * @param	arr	eg:[[1,1],[1,2],[1,3]...]
		 * @return	eg:[1,2,3]
		 */
		public function getColsScope(arr:Array):Array 
		{
			var recVo:Array = [];
			var colsArr:Array = [];
			var index:int;
			for (var name:String in arr) 
			{
				recVo = arr[name] as Array;
				if (colsArr.length <= 0) {
					colsArr.push(recVo[0]);
				}else {
					index = colsArr.indexOf(recVo[0]);
					if (index == -1) {
						colsArr.push(recVo[0]);
					}
				}
			}
			return colsArr;
		}
		/**
		 * 
		 * @param	fromArr	eg:[[1,1],[1,2],[1,3]...]
		 * @param	byArr	eg:[1,2,3]
		 * @return			eg:[[1,1],[2,2],[3,3]...]
		 */
		public function getRecVosByArr(fromArr:Array,byArr:Array,isMax:Boolean):Array
		{
			var temp:Array = [];
			var cols:int;
			var getRecByCols:Array=[];
			for (var name:String in byArr) 
			{
				cols = byArr[name] as int;
				getRecByCols=getRecVosByCols(fromArr,cols,isMax);
				if(getRecByCols.length<=0)break;
				temp.push(getRecByCols);
			}
			return temp;
		}
		/**
		 * 
		 * @param	fromArr	eg:[[1,1],[1,2],[1,3]...]
		 * @param	cols	eg:1
		 * @param	isMax	取行编号最大值
		 * @return			eg:[1,1]
		 */
		public function getRecVosByCols(fromArr:Array,cols:int,isMax:Boolean):Array
		{
			var temp:Array = [];
			var recVo:Array = [];
			var index:int;
			for (var name:String in fromArr) 
			{
				recVo = fromArr[name] as Array;
				if (recVo[0] == cols) {
					if (temp.length <= 0) {
						temp=recVo;
					}else {
						if (isMax) {
							if (recVo[1] > temp[1]) {
								temp = recVo;
							}
						}else {
							if (recVo[1] < temp[1]) {
								temp = recVo;
							}
						}
					}
				}
				
			}
			return temp;
		}
		public function getRecForMaxRow(arr:Array):int {
			var num:int;
			var recVo:Array;
			for (var name:String in arr) 
			{
				recVo = arr[name] as Array;
				if (recVo[1] > num) {
					num = recVo[1];
				}
			}
			return num;
		}
		//获取两个recVo的列间距
		public function getDis(recVo1:Array,recVo2:Array):int {
			return Math.abs(recVo1[1] - recVo2[1]);
		}
	}
	
}